// pages/my/orders/orders.js
import moment from '../../../utils/moment.js'
var app = getApp()
Page({
  data: {
    goodsInfoShow:true,
    goodsPriceShow:true,
    orderType: 0,//0 客户订单 1 待付款 2 待发货 3 已发货 4 已完成 5 退款、售后
    orders: [
    // {
    //     totalNumber: 2,
    //     orderPrice: "12233",
    //     serviceFee: 222,
    //     orderNo: "28736540188",
    //     receipt: false,//是否已收货,
    //     refundStatus: 1,//售后状态
    //     goodsList: []
    //   }
    ],
    showToast:false,
    showToastTit:'',
    showToastImg:'',
    accountId:'',
    agentId:'',
    orderTab:0
  },
  onLoad: function(options) {
    var orderType=options.orderType;
    // var orderType=2
    this.setData({
      orderType: orderType
    })
    app.getMemInfo().then(res=>{
      this.setData({
        accountId:res.data.id,
        agentId:res.data.agentId
      })
      if (orderType==0) {
        this.getOrderList(res.data.id,1)
      }else if (orderType==1) {
        this.getWaitPayList(res.data.id,res.data.agentId)
      }else if (orderType==2) {
        this.getStockOrderList(res.data.id,res.data.agentId,'1')
      }else if (orderType==3) {
        this.getStockOrderList(res.data.id,res.data.agentId,'2')
      }else if (orderType==4) {

      }else{
        this.getAfterList(res.data.id,res.data.agentId)
      }
    })
    wx.setNavigationBarTitle({
      title: options.title,
    })
  },
  viewOrderDetailTap: function (event) {
    var isReceipt=event.currentTarget.dataset.type
    var orderId = event.currentTarget.dataset.orderid
    var company = event.currentTarget.dataset.company
    var expressNo = event.currentTarget.dataset.expressNo
    let isPay = event.currentTarget.dataset.tab
    let saler = event.currentTarget.dataset.saler
    console.log(event)
    var title="订单详情"
    var type = this.data.orderType;
    if (type==0) {
      title ="客户订单详情";
      
    }
    if (type == 3 && isReceipt){
      type=4
    }
    if (type==0) {
      wx.navigateTo({
        url: '../orderDetail/orderDetail?title=' + title + "&type=" + type + "&orderId=" + orderId + '&pay=' + isPay + '&saler=' + saler
      })
    }else if(type==3){
      wx.navigateTo({
        url: '../orderDetail/orderDetail?title=' + title + "&type=" + type + "&orderId=" + orderId + "&company=" + company + "&expressNo=" + expressNo,
      })
    }else if(type!=5){
      wx.navigateTo({
        url: '../orderDetail/orderDetail?title=' + title + "&type=" + type + "&orderId=" + orderId,
      })
    }else{
      var status = event.currentTarget.dataset.refundStatus;
      var id = event.currentTarget.dataset.return;
      // wx.navigateTo({
      //   url: '../refundDetail/refundDetail?refundStatus='+status+'&returnsId='+id,
      // })
    }
  },
  //查看售后订单详情
  viewRefundDetailTap:function(e){
    var status = e.currentTarget.dataset.refundStatus;
    var id = e.currentTarget.dataset.return;
    console.log(e.currentTarget.dataset)
    wx.navigateTo({
      url: '../refundDetail/refundDetail?refundStatus='+status+'&returnsId='+id,
    })
  },
  //配送详情
  logisticsDetailTap:function(e){
    let company = e.currentTarget.dataset.company
    let expressNo = e.currentTarget.dataset.expressNo
    console.log(e.currentTarget)
    wx.navigateTo({
      url: '../logistics/logistics?company='+company+'&expressNo='+expressNo,
    })
  },
  //催单
  reminderTap:function(e){
    var that = this
    let time = e.currentTarget.dataset.time
    let begin = moment(time).add(3,'d')
    let now = moment()
    if (moment().isAfter(begin)) {  //三天后
      let data = {
        orderId:e.currentTarget.dataset.id
      }
      app.request('wx_app/agent_order/sendReminder',data,'GET').then(res=>{
        if (res.code==1) {
          this.setData({
            showToast:true,
            showToastTit:'已催促厂家发货',
            showToastImg:'../../../image/success.png'
          })
          setTimeout(function(){
            that.setData({
              showToast:false
            })
          },1500)
        }else{
          app.errorMsg(res.message)
        }
      })
    }else{  //没到三天
      this.setData({
        showToast:true,
        showToastTit:'下单后3日才可催单',
        showToastImg:'../../../image/wrong.png'
      })
      setTimeout(function(){
        that.setData({
          showToast:false
        })
      },1500)
    }
  },
  getOrderList(accountId,orderPayState){
    wx.showLoading({
      title:'正在加载'
    })
    var that = this
    let data = {
      accountId,
      orderPayState
    }
    app.request('wx_app/user_order/list',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        if (Object.keys(res.data).length==0) {
          app.errorMsg('未查询到订单')
        }else{
          let orders = []
          res.data.orders.forEach((item,i)=>{
            let goodsList = []
            let total = 0   
            item.orderDetails.forEach(res=>{
              goodsList.push({
                name:res.name,
                num:res.amount,
                url:res.pic,
                type:res.des,
                price:res.price
              })
              total += parseInt(res.amount)
            })
            orders.push({
              totalNumber:item.nums,
              orderNo:item.orderNo,
              sales_man:item.sales_man,
              orderPrice:item.orderPrice,
              goodsList:goodsList,
              orderId:item.orderId,
              total
            })
          })  
          that.setData({
            orders
          })
          wx.hideLoading()
        }
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  //待发货和已发货
  getStockOrderList(accountId,agentId,orderState){
    wx.showLoading({
      title:'正在加载'
    })
    let data = {
      accountId,
      agentId,
      orderState
    }
    var that = this
    console.log(data)
    let orders = []
    app.request('wx_app/agent_order/getStockOrderList',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        if (Object.keys(res.data).length==0) {
          app.errorMsg('未查询到订单')
        }else{
          res.data.orders.forEach(item=>{
            let orderDetails = []
            let total = 0
            item.orderDetails.forEach(data=>{
              orderDetails.push({
                name:data.name,
                url:data.pic,
                type:data.versionValueDesc,
                num:data.amount,
                price:data.orderPrice
              })
              total += parseInt(data.amount)
            })
            orders.push({
              orderNo:item.orderNo,
              orderPrice:item.orderPrice,
              orderId:item.orderId,
              created:item.created,
              goodsList:orderDetails,
              total,
              receipt:item.orderState==4?true:false,
              company:item.company,
              expressNo:item.expressNo,
              logisticsType:item.logisticsType
            })
          })
          that.setData({
            orders
          })
          wx.hideLoading()
        }
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  getWaitPayList(accountId,agentId){
    wx.showLoading({
      title:'正在加载'
    })
    let data = {
      accountId,
      agentId,
      orderPayState : 1
    }
    var that = this
    let orders = this.data.orders
    app.request('wx_app/agent_order/wait_list',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        if (Object.keys(res.data).length==0) {
          app.errorMsg('未查询到订单')
        }else{
          res.data.orders.forEach(item=>{
            let orderDetails = []
            let total = 0
            item.orderDetails.forEach(data=>{
              orderDetails.push({
                name:data.name,
                url:data.pic,
                type:data.des,
                num:data.amount,
                price:data.price
              })
              total += parseInt(data.amount)
            })
            orders.push({
              orderNo:item.orderNo,
              orderPrice:item.orderPrice,
              orderId:item.orderId,
              created:item.created,
              goodsList:orderDetails
            })
          })
          that.setData({
            orders
          })
          wx.hideLoading()
        }
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  //售后列表
  getAfterList(accountId,agentId){
    let data = {
      accountId,
      agentId
    }
    var that = this
    let orders = this.data.orders
    app.request('wx_app/after/sale/list',data,'GET').then(res=>{
      console.log(res)
      res.data.order_returns.forEach(item=>{
        let orderDetails = []
        orderDetails.push({
          name:item.productName,
          url:item.imageUrl,
          type:item.versionDesc,
          num:item.amount,
          price:item.price,
        })
        let refundStatus = ''

        if (item.status==-1) { //审核拒绝
          refundStatus = 3  //卖家已审核未通过
        }else if (item.status==0) { //未审核
          refundStatus = 1 //已申请退款，等待卖家审核
        }else if (item.status==1) { //审核通过
          if (item.productStatus==1) { //未到货
            refundStatus = 4  //等待卖家收货
          }else if (item.productStatus==2) { //未发货
            refundStatus = 2 //卖家已审核通过，等待买家寄回商品
          }else if (item.productStatus==0) { //已收到货
            if (item.refundState==-1) {
              refundStatus = 6 //拒退款
            }else if (item.refundState==1) {
              refundStatus = 5 //已退款
            }else if (item.refundState==0) {
              refundStatus = 4 //未退款
            } 
          }
        }
        orders.push({
          goodsList:orderDetails,
          returnsId:item.returnsId,
          status:item.status,
          refundStatus
        })
        that.setData({
            orders
          })
      })
    })
  },
  bindConfirmReceipt(e){
    console.log(e.currentTarget.dataset.id)
    var that = this
    let data = {
      orderId:e.currentTarget.dataset.id
    }
    app.request('wx_app/agent_order/confirmReceipt',data,'GET').then(res=>{
      if (res.code==1) {
        wx.showToast({
          icon:'success',
          title:'确认收货成功'
        })
        setTimeout(function(){
          that.getStockOrderList(that.data.accountId,that.data.agentId,'2')
        },1500)
      }
    })
  },
  changeType(e){
    let status = e.currentTarget.dataset.paystatus
    this.setData({
      orderTab:status
    })
    if (status==0) {
      this.getOrderList(this.data.accountId,1)
    }else{
      this.getOrderList(this.data.accountId,2)
    }
  }
})